﻿// Copyright (c) egmkang wang. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace XServer.Message
{
    using System;
    using System.Collections.Generic;
    using MessagePack;

    /// <summary>
    /// 注意:
    /// 1. Req是request的缩写, Rsp是response的缩写
    /// 2. 每个req对应一个rsp
    /// 3. req和rsp必须是partial class, 方便客户端服务器写自己的扩展, 必须加MsgID属性
    /// 4. 可以序列化的东西需要添加MessagePackObject属性
    /// 5. 需要序列化的字段前面增加Key(N)属性, N作为该字段的唯一键, 字段类型发生变化, 需要改变N
    /// </summary>

    #region 消息ID定义
    public enum MsgID
    {
        MSG_REQ_HEART_BEAT      = 0x2000,   //心跳请求
        MSG_RSP_HEART_BEAT      = 0x2001,   //心跳请求返回
        MSG_REQ_LOGIN           = 0x2002,   //登录
        MSG_RSP_LOGIN           = 0x2003,   //登录返回
        MSG_REQ_CREATE_PLAYER   = 0x2004,   //创建玩家
        MSG_RSP_CREATE_PLAYER   = 0x2005,   //创建玩家返回
        MSG_REQ_LOAD_PLAYER     = 0x2006,   //拉取玩家信息
        MSG_RSP_LOAD_PLAYER     = 0x2007,   //拉取玩家信息返回
        MSG_REQ_LOAD_RANK_LIST  = 0x2008,   //拉取排行榜 
        MSG_RSP_LOAD_RANK_LIST  = 0x2009,   //拉取排行榜返回
        MSG_RSP_ERROR           = 0x200A,   //错误信息


    }
    #endregion


    [MsgID(MsgID.MSG_REQ_HEART_BEAT), MessagePackObject]
    public partial class MsgReqHeartBeat : MessageBase<MsgReqHeartBeat>
    {
        [Key(0)]
        public long MilliSeconds;
    }

    [MsgID(MsgID.MSG_RSP_HEART_BEAT), MessagePackObject]
    public partial class MsgRspHeartBeat : MessageBase<MsgRspHeartBeat>
    {
        [Key(0)]
        public long MilliSeconds;

        [Key(1)]
        public long ServerSeconds;
    }

    [MsgID(MsgID.MSG_REQ_LOGIN), MessagePackObject]
    public partial class MsgReqLogin : MessageBase<MsgReqLogin>
    {
        [Key(0)]
        public string OpenID;
        [Key(1)]
        public long ServerID;
    }

    [MsgID(MsgID.MSG_RSP_LOGIN), MessagePackObject]
    public partial class MsgRspLogin : MessageBase<MsgRspLogin>
    {
        [Key(0)]
        public string OpenID;
        [Key(1)]
        public long PlayerID;
        [Key(2)]
        public long ServerID;
        [Key(3)]
        public long ExpiryTime;
        [Key(4)]
        public string Token;
    }

    [MsgID(MsgID.MSG_REQ_CREATE_PLAYER), MessagePackObject]
    public partial class MsgReqCreatePlayer : MessageBase<MsgReqCreatePlayer>
    {
        [Key(0)]
        public string Name;
    }

    [MsgID(MsgID.MSG_RSP_CREATE_PLAYER), MessagePackObject]
    public partial class MsgRspCreatePlayer : MessageBase<MsgRspCreatePlayer>
    {
        [Key(0)]
        public string Name;
    }

    [MessagePackObject]
    public partial class Item
    {
        [Key(0)]
        public long Uid;
        [Key(1)]
        public long Count;
        [Key(2)]
        public int ItemID;

        [Key(3)]
        public List<ValueTuple<int, int>> Attr
        {
            get { return this.attr; }
            set { this.attr.Clear();this.attr.AddRange(value); }
        }

        private readonly List<ValueTuple<int, int>> attr = new List<ValueTuple<int, int>>();
    }

    [MessagePackObject]
    public partial class PlayerSimpleInfo
    {
        [Key(0)]
        public long Uid;
        [Key(1)]
        public string Name;
        [Key(2)]
        public int Level;
        [Key(3)]
        public int VipLevel;
        [Key(4)]
        public long FightAttr;
    }

    [MessagePackObject]
    public partial class FriendInfo
    {
        [Key(0)]
        public string Name;
        [Key(1)]
        public long PlayerID;
        [Key(2)]
        public long LastOnlineTime;
        [Key(3)]
        public int Level;
        [Key(4)]
        public int VipLevel;
        [Key(5)]
        public int avatar;
        [Key(6)]
        public int rank_id;
    }

    [MessagePackObject]
    public partial class OtherPlayerInfo
    {
        [Key(0)]
        public long PlayerID;
        [Key(1)]
        public int Level;
        [Key(2)]
        public int VipLevel;
    }

    [MessagePackObject]
    public partial class RankItemInfo
    {
        public RankItemInfo()
        {
            this.Name = "";
            this.ArmyName = "";
        }
        [Key(0)]
        public long Uid { get; set; }
        [Key(1)]
        public string Name { get; set; }
        [Key(2)]
        public long UpdateTime { get; set; }
        [Key(3)]
        public string ArmyName { get; set; }
        [Key(4)]
        public int Avatar { get; set; }
        [Key(5)]
        public long V1 { get; set; }
        [Key(6)]
        public long V2 { get; set; }
        [Key(7)]
        public long V3 { get; set; }
        [Key(8)]
        public long V4 { get; set; }
    }

    [MsgID(MsgID.MSG_REQ_LOAD_PLAYER), MessagePackObject]
    public partial class MsgReqLoadPlayer : MessageBase<MsgReqLoadPlayer>
    {
    }

    [MsgID(MsgID.MSG_RSP_LOAD_PLAYER), MessagePackObject]
    public partial class MsgRspLoadPlayer : MessageBase<MsgRspLoadPlayer>
    {
    }

    [MsgID(MsgID.MSG_RSP_ERROR), MessagePackObject]
    public partial class MsgNotifyError : MessageBase<MsgNotifyError>
    {
        [Key(0)]
        public ushort MsgId;
        [Key(1)]
        public int ErrCode;
    }

    [MsgID(MsgID.MSG_REQ_LOAD_RANK_LIST), MessagePackObject]
    public partial class MsgReqLoadRankList : MessageBase<MsgReqLoadRankList>
    {
        [Key(0)]
        public int RankType;
    }

    [MsgID(MsgID.MSG_RSP_LOAD_RANK_LIST), MessagePackObject]
    public partial class MsgRspLoadRankList : MessageBase<MsgRspLoadRankList>
    {
        [Key(0)]
        public int RankType;
        [Key(1)]
        public List<RankItemInfo> RankList
        {
            get => this.list;
            set { this.list.Clear(); this.list.AddRange(value); }
        }

        private List<RankItemInfo> list = new List<RankItemInfo>();
    }

}
